package algorithm.easy;

public class Solution21 {

    //21 合并两个有序链表
    /*
        //输入：l1 = [1,2,4], l2 = [1,3,4]
        //输出：[1,1,2,3,4,4]
     */
    public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode p = list1;
        ListNode q = list2;
        ListNode listNode = new ListNode(0);
        ListNode s = listNode;
        while(p!=null && q!=null){
            if(p.val<q.val){
                s.next = p;
                p = p.next;
            }else{
                s.next = q;
                q = q.next;
            }
            s = s.next;
        }
        if(p!=null){
            s.next = p;
        }else{
            s.next = q;
        }
        listNode.next.print();
        return listNode.next;
    }

    public static void main(String[] args){
        //21.合并两个有序链表
        ListNode listNode1 = ListNode.init1();
        ListNode listNode2 = ListNode.init2();
        mergeTwoLists(listNode1,listNode2);
    }
}
